﻿using PelitaProduksi.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PelitaProduksi.View.Panel.Master
{
    public partial class MasterBarang : Form
    {
        /* Master form */
        int max = 0;
        int highest = 0;
        List<Barang> list;
        bool aktifOnly = false;
        /**/
        Main main;
        int code;

        public MasterBarang(Main main, int code)
        {
            InitializeComponent();
            this.main = main;
            this.code = code;
            init();
        }

        private void init()
        {
            this.list = Barang.listAllBarang(aktifOnly);
            foreach (Barang b in this.list)
            {
                DataGridViewRow row = new DataGridViewRow();
                data.Rows.Add(b.Kode, b.Nama, b.Aktif, b.Keterangan, b.Tupdate, b.Jupdate, b.Usrupdate);
                highest = int.Parse(b.Kode);
                if (max < highest) max = highest;
            }
        }

        private void Reset()
        {
            if (data != null)
                data.Rows.Clear();
            init();
        }

        private bool Save()
        {
            try
            {
                List<Barang> list = new List<Barang>();
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    // Invalid input, ignore
                    if (data.Rows[i].Cells["kode"].Value == null) continue;
                    if (data.Rows[i].Cells["nama"].Value == null)
                    {
                        MessageBox.Show("Nama tidak boleh kosong");
                        return false;
                    }
                    string keterangan = null;
                    if (data.Rows[i].Cells["keterangan"].Value != null)
                    {
                        keterangan = data.Rows[i].Cells["keterangan"].Value.ToString();
                    }
                    Barang b = new Barang();
                    b.Kode = data.Rows[i].Cells["kode"].Value.ToString();
                    b.Nama = data.Rows[i].Cells["nama"].Value.ToString();
                    b.Keterangan = keterangan;
                    if (data.Rows[i].Cells["aktif"].Value != null)
                        b.Aktif = data.Rows[i].Cells["aktif"].Value.ToString() != "0";
                    else b.Aktif = false;
                    b.Tupdate = DateTime.Now.ToShortDateString();
                    b.Jupdate = DateTime.Now.ToShortTimeString();
                    b.Usrupdate = this.main.USER;
                    /* Check which row is changed */
                    foreach (Barang pp in this.list)
                    {
                        if (pp.Kode.Equals(b.Kode))
                        {
                            if (!pp.Equals(b))
                            {
                                list.Add(b);
                                break;
                            }
                        }
                    }
                }
                /* Query into database */
                foreach (Barang b in list)
                {
                    if (int.Parse(b.Kode) < highest)
                    {
                        // Edit
                        b.edit(b.Kode);
                    }
                    else
                    {
                        // Add
                        b.insert();
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Save
            if (Save())
            {
                MessageBox.Show("Data berhasil disimpan");
            }
            else
            {
                MessageBox.Show("Data Gagal disimpan", "Error Saving", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Reset
            Reset();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // Close
            this.Close();
        }

        private void data_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (data.CurrentRow.Cells["kode"].Value == null)
            {
                data.CurrentRow.Cells["kode"].Value = max++;
                data.CurrentRow.Cells["aktif"].Value = 1;
            }
        }

        private void MasterBarang_FormClosed(object sender, FormClosedEventArgs e)
        {
            main.Visible = true;
            main.Activate();
            main.BringToFront();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1 != null)
                aktifOnly = checkBox1.Checked;
            Reset();
            init();
        }
    }
}
